#include <event2/event.h>
#include <stdio.h>

// struct event *ev;
// struct timeval tv;
// void time_cb(int fd, short event, void *argc){
//     printf("timer wakeup/n");
//     evtimer_add(ev, &tv); // reschedule timer
// }
// 
// int main(){
//     struct event_base *base = event_base_new();
//     tv.tv_sec = 10; // 10s period
//     tv.tv_usec = 0;
//     evtimer_new(base, time_cb, NULL);
//     evtimer_add(ev, &tv);
//     event_base_dispatch(base);
//     return 0;
// }

static int n_calls = 0;
struct timeval one_sec = { 1, 0 };
void cb_func(evutil_socket_t fd, short what, void *arg)
{
    struct event *me = arg;

    printf("cb_func called %d times so far.\n", ++n_calls);

    if (n_calls > 100)
       event_del(me);
    evtimer_add(me, &one_sec);
}

void run(struct event_base *base)
{
    struct event *ev;
    /* We're going to set up a repeating timer to get called called 100
       times. */
//     ev = event_new(base, -1, EV_PERSIST, cb_func, event_self_cbarg());
    ev = evtimer_new(base, cb_func, event_self_cbarg());
    event_add(ev, &one_sec);
    event_base_dispatch(base);
}

int main(){
    struct event_base *base = event_base_new();
    run(base);
    return 0;
}
